import { createVNode } from "vue";
import { ButtonGroupVue } from ".";
import { BaseComponent } from "../type";
import { ButtonGroupOptions } from "./type";

export class ButtonGroup implements BaseComponent {
  // 按钮props数据
  buttons: ButtonGroupOptions[] = [];
  constructor(buttons?: ButtonGroupOptions[]) {
    if (buttons) {
      this.buttons = buttons;
    }
  }
  /** 新增按钮 */
  addButton(options: ButtonGroupOptions) {
    const btn = this.buttons.find((e) => e.name == options.name);
    if (!btn) this.buttons.push(options);
  }
  /** 移除按钮 */
  removeButton(index: number): void {
    this.buttons.splice(index, 1);
  }
  /** 设置按钮禁用 */
  setDisabled(index: number, state: boolean) {
    const btn = this.buttons[index];
    if (btn) {
      btn.disabled = state;
    } else {
      throw new Error(`请传入正确的下标index:${index}`);
    }
  }
  clear() {
    this.buttons = [];
  }
  getVNode() {
    return createVNode(ButtonGroupVue, { options: this.buttons });
  }
}
